<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

class frontenduseraccessModelMenuaccess extends JModel
{	
	var $usergroups;
	var $config;
	var $menu_items;
	var $fua_current_menu_type;	
	var $current_parent;
	var $page_access;
	var $menu_types;
	
	function fua_page_access_init($fua_usergroups, $fua_config, $fua_menu_items, $access_menuitems){
		
		$this->usergroups = $fua_usergroups;		
		$this->config = $fua_config;		
		$this->menu_items = $fua_menu_items;
		$this->page_access = $access_menuitems;		
		
		$database = JFactory::getDBO();	
				
		$database->setQuery("SELECT title, menutype FROM #__menu_types ORDER BY title ASC"  );
		$menutypes = $database-> loadObjectList();
		$menutypes_db = array();	
		foreach($menutypes as $menutype_db){	
			$fixed_type = str_replace('-','separator',$menutype_db->menutype);	
			$fixed_type = str_replace(' ','spacer',$fixed_type);				
			$new_menutype = array($fixed_type,$menutype_db->title);
			array_push($menutypes_db, $new_menutype);					
		}
		$this->menu_types = $menutypes_db;		
	}
	
	function fetch_menuitems($id, $menu_type) {		
		$menu = null;
		$menu = array();
		
		foreach($this->menu_items as $menuItem){
			$menuItem_type = str_replace('-','separator',$menuItem->menutype);	
			if($menuItem->parent==$id && $menuItem_type==$menu_type){
				$itemArray = array($menuItem->id, $menuItem->name, $menuItem->parent, $menuItem->access);				
				array_push($menu, $itemArray);
			}
		}	
		return $menu;
	}

	function look_for_children($menu,$level) {
		if (!is_array($menu)) return;  
		$level = $level + 1;
		foreach ($menu as $m){
			$this->show_menu_item($m,$level);
		}  
		$level = $level - 1;   	
	}
	
	function show_menu_item($m,$level) {		
		$padding = $level*2;
		echo '<tr class="row1"><td style="text-align: left; padding-left: '.$padding.'0px">';
		echo $m[1];		
		echo '</td>';
		if($this->config['display_joomla_access_levels']){
			echo '<td>';			
			if($m[3]=='0'){
				echo '<span style="color: green;">'.JText::_('PUBLIC').'</span>';
			}elseif($m[3]=='1'){				
				echo '<span style="color: red;">'.JText::_('REGISTERED').'</span>';
			}elseif($m[3]==2){				
				echo '<span style="color: black;">'.JText::_('SPECIAL').'</span>';
			}
			echo '</td>';		
		}
		foreach($this->usergroups as $usergroup){
			$checked = '';
			$checked_hidden = '';
			if (in_array($m[0].'_'.$usergroup->id, $this->page_access)) {
				$checked = 'checked="checked"';
				$checked_hidden = '1';
			}
			echo '<td align="center"><input type="hidden" name="menu_access_hidden[]" id="'.$m[0].'__'.$usergroup->id.'__hidden" value="'.$m[0].'__'.$usergroup->id.'__hidden__'.$checked_hidden.'" /><input type="checkbox" name="menu_access[]" id="'.$m[0].'_0_'.$usergroup->id.'" onclick="toggle_right(\''.$m[0].'__'.$usergroup->id.'__hidden\');" value="'.$m[0].'__'.$usergroup->id.'" '.$checked.' /></td>';
		}
		echo '</tr>';
		if ($menu = $this->fetch_menuitems($m[0],$this->fua_current_menu_type)){
			$this->look_for_children($menu,$level);
		}
	}
	
	function fua_echo_menus(){
		$colspan = count($this->usergroups)+1;
		if($this->config['display_joomla_access_levels']){
			$colspan = $colspan+1;
		}
		for($n = 0; $n < count($this->menu_types); $n++){			
			echo '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';			
			echo '<tr><td style="font-size: 1.5em; font-weight: bold;text-align: left;" colspan="'.$colspan.'">';
			echo $this->menu_types[$n][1];
			echo '</td></tr>';
			$this->fua_current_menu_type = $this->menu_types[$n][0];
			echo '<tr class="row1"><td>'.JText::_('SELECTALL').'</td>';
			if($this->config['display_joomla_access_levels']){
				echo '<td>&nbsp;</td>';
			}
			foreach($this->usergroups as $usergroup){
				echo '<td align="center">';
				echo '<input type="checkbox" name="checkall[]" value="" id="'.$this->fua_current_menu_type.'_'.$usergroup->id.'" onclick="select_all(\'menuarray_'.$this->fua_current_menu_type.'\','.$usergroup->id.',this.id);" />';
				echo '</td>';
			}
			echo '</tr>';				
			$this->look_for_children($this->fetch_menuitems(0,$this->fua_current_menu_type),0);
		}
	}
}
?>